package com.hks360.car_treasure.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.activity.MainActivity;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.model.Add.User;
import com.hks360.car_treasure.mvp.user.presenter.UserPresenter;
import com.hks360.car_treasure.mvp.user.presenter.UserPresenterImpl;
import com.hks360.car_treasure.mvp.user.view.UserView;
import com.hks360.car_treasure.util.MyUtil;
import com.hks360.car_treasure.util.TimerUtil;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.RegexUtil;
import com.hks360.library.util.UIUtil;

/**
 * 用户注册
 */
public class RegisterFragment extends BaseFragment implements UserView {
    private EditText mPhoneEt;
    private EditText mCodeEt;
    private EditText mPasswordEt;
    private EditText mPassAgainEt;
    private TextView mGetCodeTv;
    private TextView mRegisterTv;

    private UserPresenter mUserPresenter;

    @Override
    public void setupView() {
        mPhoneEt = UIUtil.findViewById(mRootView, R.id.regiester_phone_et);
        mCodeEt = UIUtil.findViewById(mRootView, R.id.register_code_et);
        mGetCodeTv = UIUtil.findViewById(mRootView, R.id.register_getcode_tv);
        mPasswordEt = UIUtil.findViewById(mRootView, R.id.register_pass_et);
        mPassAgainEt = UIUtil.findViewById(mRootView, R.id.register_passagain_et);
        mRegisterTv = UIUtil.findViewById(mRootView, R.id.register_tv);
    }

    public void clearView() {
        mPhoneEt.setText(null);
        mPasswordEt.setText(null);
        mCodeEt.setText(null);
        mPassAgainEt.setText(null);
    }

    @Override
    public void sendCheckCodeFailed() {
        TimerUtil.timerCancel();
        mGetCodeTv.setText("获取验证码");
        mGetCodeTv.setEnabled(true);
    }

    public boolean isInputError() {
        if (CommonUtil.isEmptyEt(mPhoneEt) || CommonUtil.isEmptyEt(mPasswordEt) || CommonUtil.isEmptyEt(mCodeEt)) {
            CommonUtil.showToast(getContext(), "请您把信息填写完整后再按注册按钮");
            return true;
        }
        if(!RegexUtil.checkPhone(mPhoneEt.getText().toString().trim())) {
            CommonUtil.showToast(getContext(),"手机号输入格式不正确");
            return true;
        }

        if (mPasswordEt.getText().toString().length() > 20 || mPasswordEt.getText().toString().length() < 6) {
            CommonUtil.showToast(getContext(), "请您按提示输入密码位数");
            return true;
        }

        if (!mPasswordEt.getText().toString().trim().equals(mPassAgainEt.getText().toString().trim())) {
            CommonUtil.showToast(getContext(), "两次输入的密码不一致");
            mPassAgainEt.setText(null);
            return true;
        }
        return false;
    }

    @Override
    public void initData() {
        mUserPresenter = new UserPresenterImpl(getContext(),this);
    }

    @Override
    public void addListener() {
        mGetCodeTv.setOnClickListener(this);
        mRegisterTv.setOnClickListener(this);
    }

    @Override
    public void initView() {
        initDialog(R.string.loading_submit);
        setupView();
        initData();
        addListener();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_register, null);
        initView();
        return mRootView;
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()) {
            case R.id.register_getcode_tv:
                if (!CommonUtil.isEmptyEt(mPhoneEt)) {
                    mGetCodeTv.setEnabled(false);
                    TimerUtil.createTimer(60000, 1000, mGetCodeTv);
                    mUserPresenter.sendCheckCode(R.id.register_getcode_tv);
                } else {
                    CommonUtil.showToast(getContext(), "请先输入您的手机号再发送验证码请求");
                }
                break;
            case R.id.register_tv:
                if (!isInputError()) {
                    sendCheckCodeFailed();
                    mUserPresenter.regist(R.id.register_tv, R.id.login_tv);
                }
                break;
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        sendCheckCodeFailed();
    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        mDialog.dismiss();
    }

    @Override
    public String getMobile() {
        return mPhoneEt.getText().toString().trim();
    }

    @Override
    public String getCode() {
        return mCodeEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return mPasswordEt.getText().toString().trim();
    }

    @Override
    public void loginSuccess(User user) {
        PrefUtil.putString(getContext(),PrefKey.LOGIN_PHONE,getMobile());
//        PrefUtil.putString(getContext(),PrefKey.TBOX_ID,user.getTboxid());
        getActivity().finish();
        CommonUtil.openActivity(getContext(), MainActivity.class);
    }

    @Override
    public void showToastMsg(String msg) {
        CommonUtil.showToast(getContext(),msg);
    }

    @Override
    public void sendCheckCodeSuccess() {

    }

    @Override
    public void findPasswordSuccess() {

    }

}
